import unittest

import requests

from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from api.api_trust_recharge import ApiTrustRecharge
from util import parser_html, clear_data


class TestTenderList(unittest.TestCase):
    # 初始化
    def setUp(self) -> None:
        # 获取session
        self.session = requests.session()
        # 获取ApiRegisterLogin实例
        self.reg = ApiRegisterLogin(self.session)
        # 获取ApiTrustRecharge实例
        self.trust = ApiTrustRecharge(self.session)
        # 获取ApiTender实例
        self.tender = ApiTender(self.session)

    # 结束
    def tearDown(self) -> None:
        # 关闭session
        self.session.close()

    @classmethod
    def tearDownClass(cls) -> None:
        clear_data()

    # 测试方法
    def test_tender_list(self):
        # 1、获取注册图片验证码
        self.reg.api_img_code(123123)
        # 2、获取短信验证码
        self.reg.api_phone_code("13600001115", "8888")
        # 3、注册
        self.reg.api_register(phone="13600001115", password="test123", verifycode=8888, phone_code="666666")
        # 4、登录
        self.reg.api_login()
        # 5、认证
        self.trust.api_approve("李四", "110101199010038976")
        # 6、后台开户
        result = self.trust.api_trust()
        # 7、三方开户
        r = parser_html(result)
        result = self.session.post(url=r[0], data=r[1])
        print("--" * 30)
        print("三方开户的结果为：", result.text)
        self.assertIn("OK", result.text)

        # 8、获取充值图片验证码
        self.trust.api_img_code(123123)
        # 9、后台充值
        result = self.trust.api_recharge(8888)
        # 10、三方充值
        r = parser_html(result)
        result = self.session.post(url=r[0], data=r[1])
        print("--" * 30)
        print("三方充值的结果为：", result.text)
        self.assertIn("OK", result.text)

        # 11、后台投资
        result = self.tender.api_tender(642, 100)
        # 12、三方投资
        r = parser_html(result)
        result = self.session.post(url=r[0], data=r[1])
        print("--" * 30)
        print("三方投资的结果为：", result.text)
        self.assertIn("OK", result.text)
